package com.wofish.oauth.interceptor;

import com.wofish.common.core.domain.JsonRsp;
import com.wofish.oauth.exception.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 */
@ControllerAdvice
public class GloablExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public JsonRsp handleException(Exception e) {
        String msg = e.getMessage();
        if (msg == null || msg.equals("")) {
            msg = "服务器出错";
        }

        JsonRsp jsonRsp = new JsonRsp();
        jsonRsp.setCode(1);
        if(e instanceof UnauthorizedException){
            jsonRsp.setCode(((UnauthorizedException) e).getValue());
        }
        jsonRsp.setMsg(msg);
        return jsonRsp;
    }
}
